package kpi.java_training.testing.questions;

import kpi.java_training.testing.education.TypeScore;
import kpi.java_training.testing.services.StringServices;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * class implements interface for list of questions
 */
public class QuestionList {
    public final int MAX_LEVEL = 3;
    private List<Question> questions;
    private TypeScore typeScore;
    private int currentNumber;
    public QuestionList(String type) {
        questions = new ArrayList<Question>();
        typeScore = new TypeScore(type,0,0);
        currentNumber = -1;
    }

    public void add(Question item) {
        questions.add(item);
    }
    public String getType() {
        return typeScore.getType();
    }
    public int findQuestionFromPosition(int currPosition) {
        while(currPosition < questions.size()-1) {
            currPosition++;
            if (questions.get(currPosition).getLevel() == typeScore.getCurrentLevel()) {
                return currPosition;
            }
        }
        return -1;
    }
    public String findNextQuestion() {
        if (!isTestPassed()) {
            int newCurrentNumber = findQuestionFromPosition(currentNumber);
            if(newCurrentNumber != -1) {
                currentNumber = newCurrentNumber;
                return getQuestionText();
            }
            newCurrentNumber = findQuestionFromPosition(-1);
            if(newCurrentNumber != -1) {
                currentNumber = newCurrentNumber;
                return getQuestionText();
            }
            System.out.println("error: no such questions");
            return "";
        }           else {
            System.out.println("this test have already been  passed");
            return "";
        }
    }
    public String getQuestionText() {
        return questions.get(currentNumber).toString();
    }

    public String getQuestionMessage() {
        return findNextQuestion();
    }
    public boolean isTestPassed() {
        if (typeScore.getCurrentLevel() > MAX_LEVEL) {
            return true;
        }
        return false;
    }
    public boolean isCorrect() {
        if (questions.get(currentNumber).isCorrect()) {
            return true;
        }
        return false;
    }
    public int getScore() {
        int score = 0;
        for (Question q : questions) {
            if (q.isCorrect()) {
                score++;
            }
        }
        return score;
    }

    public void setQuestionAnswer(String answer) {
        questions.get(currentNumber).setAnswer(answer);
        typeScore.newQuestionAnswered(isCorrect());
    }

    public String getQuestionsSaveData() {
        String str = Integer.toString(questions.size());
        for (Question q : questions) {
            str = StringServices.makeString(str, q.getQuestionSaveData());
        }
        return str;
    }

    public String getAnswersSaveData(int answersNumber) {
        if (answersNumber < 0) {
            answersNumber = 0;
        }
        if (answersNumber > questions.size()) {
            answersNumber = questions.size();
        }
        String str = Integer.toString(answersNumber);
        for (int i = 0; i < answersNumber; i++) {
            str = StringServices.makeString(str, questions.get(i).getAnswerSaveData());
        }
        return str;
    }

    public int length() {
        return questions.size();
    }
    public int getLevel() {
        return typeScore.getCurrentLevel();
    }
    private boolean inRange(int questionNumber) {
        return (questionNumber >= 0) && (questionNumber < questions.size());
    }

    public void shuffle() {
        Collections.shuffle(questions);
    }
}
